capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/*
Figure B5
*/

local data "" ;
local output "" ;

use "dob70s_outcomes_18_38.dta", clear ;	

local out_list "charge_18_to_38 charge_18plus charge_ever charge_incgen_18_to_38 charge_incgen_18plus charge_incgen_ever 
			hybrid_incar_18_to_38 hybrid_incar_18plus hybrid_incar_ever" ;
	
rename date_of_birth dob ;

gen dobdd_stata = mdy(month(dob),day(dob),year(dob)+18) ;
gen dobdd_run = dobdd_stata - mdy(08,22,1996) ;	
gen dobmo_stata = ym(year(dob)+18,month(dob)) ;
gen dobmo_run = dobmo_stata - mofd(mdy(08,22,1996)) ;
gen dobwk_stata	=wofd(mdy(month(dob),day(dob),year(dob)+18));
gen dobwk_run	=dobwk_stata-wofd(mdy(08,22,1996));

gen byte n_post = (dobdd_run > 0) ;
gen postXdobdd  = n_post * dobdd_run ;

gen char_18_38 = charge_18_to_38 ;
gen hybinc_18_38 = hybrid_incar_18_to_38 ;   
			
foreach bwidth in 150 200 250 300 { ;
	foreach out in char_18_38 hybinc_18_38 { ;
		reg `out' n_post dobdd_run postXdobdd if abs(dobdd_run) <= `bwidth' ;
		gen `out'_`bwidth'_cons_b = _b[_cons] ;
		gen `out'_`bwidth'_cons_se = _se[_cons] ;
		gen st_`out'_`bwidth'_cons_b = 2 * (normal(-abs(`out'_`bwidth'_cons_b/`out'_`bwidth'_cons_se))) ;		
		gen sg_`out'_`bwidth'_cons_b = "*" if st_`out'_`bwidth'_cons_b <= 0.1 & st_`out'_`bwidth'_cons_b > 0.05 ;						
		replace sg_`out'_`bwidth'_cons_b = "**" if st_`out'_`bwidth'_cons_b <= 0.05 & st_`out'_`bwidth'_cons_b > 0.01 ;								
		replace sg_`out'_`bwidth'_cons_b = "***" if st_`out'_`bwidth'_cons_b <= 0.0;								
			foreach var in n_post dobdd_run postXdobdd  { ;
				gen `out'_`bwidth'_`var'_b = _b[`var'] ;
				gen `out'_`bwidth'_`var'_se = _se[`var'] ;
				gen st_`out'_`bwidth'_`var'_b = 2 * (normal(-abs(`out'_`bwidth'_`var'_b/`out'_`bwidth'_`var'_se))) ;
				gen sg_`out'_`bwidth'_`var'_b = "*" if st_`out'_`bwidth'_`var'_b <= 0.1 & st_`out'_`bwidth'_`var'_b > 0.05 ;
				replace sg_`out'_`bwidth'_`var'_b = "**" if st_`out'_`bwidth'_`var'_b <= 0.05 & st_`out'_`bwidth'_`var'_b > 0.01 ;		
				replace sg_`out'_`bwidth'_`var'_b = "***" if st_`out'_`bwidth'_`var'_b <= 0.01 ;				
				} ;		
	} ;
} ;

 
/* Exporting results to table */

preserve;
collapse (sum) `out_list', by(dobdd_run) ;
foreach bwidth in 150 200 250 300 { ;
	foreach out in charge_18_to_38 hybrid_incar_18_to_38 { ;
		summ `out' if abs(dobdd_run) <= `bwidth' ; 
		gen n_`out'_`bwidth' = r(sum) ;
	} ;
} ;	

putexcel set "`output'/figB5.xls", sheet("Sheet1") replace ;

putexcel A12 = "N (individuals)" ;

putexcel B12 = n_charge_18_to_38_150 ;
putexcel D12 = n_hybrid_incar_18_to_38_150 ;
putexcel F12 = n_charge_18_to_38_200 ;
putexcel H12 = n_hybrid_incar_18_to_38_200 ;
putexcel J12 = n_charge_18_to_38_250 ;
putexcel L12 = n_hybrid_incar_18_to_38_250 ;
putexcel N12 = n_charge_18_to_38_300 ;
putexcel P12 = n_hybrid_incar_18_to_38_300 ;
restore ; 

putexcel set "`output'/figB5.xls", sheet(Sheet1) modify ;

putexcel B2 = "150-day bwidth" ;
putexcel B3 = "Charges" ;
putexcel D2 = "150-day bwidth" ;
putexcel D3 = "Incarceration" ;
putexcel F2 = "200-day bwidth" ;
putexcel F3 = "Charges" ;
putexcel H2 = "200-day bwidth" ;
putexcel H3 = "Incarceration" ;
putexcel J2 = "250-day bwidth" ;
putexcel J3 = "Charges" ;
putexcel L2 = "250-day bwidth" ;
putexcel L3 = "Incarceration" ;
putexcel N2 = "300-day bwidth" ;
putexcel N3 = "Charges" ;
putexcel O2 = "300-day bwidth" ;
putexcel O3 = "Incarceration" ;

putexcel A4 = "Post" ;
putexcel A6 = "DOB" ;
putexcel A8 = "Post x DOB" ;
putexcel A10 = "Constant" ;
  
putexcel B4 = char_18_38_150_n_post_b ;
putexcel B5 = char_18_38_150_n_post_se ;
putexcel C4 = sg_char_18_38_150_n_post_b ;
putexcel D4 = hybinc_18_38_150_n_post_b ;
putexcel D5 = hybinc_18_38_150_n_post_se ;
putexcel E4 = sg_hybinc_18_38_150_n_post_b ;
putexcel F4 = char_18_38_200_n_post_b ;
putexcel F5 = char_18_38_200_n_post_se ;
putexcel G4 = sg_char_18_38_200_n_post_b ;
putexcel H4 = hybinc_18_38_200_n_post_b ;
putexcel H5 = hybinc_18_38_200_n_post_se ;
putexcel I4 = sg_hybinc_18_38_200_n_post_b ;
putexcel J4 = char_18_38_250_n_post_b ;
putexcel J5 = char_18_38_250_n_post_se ;
putexcel K4 = sg_char_18_38_250_n_post_b ;
putexcel L4 = hybinc_18_38_250_n_post_b ;
putexcel L5 = hybinc_18_38_250_n_post_se ;
putexcel M4 = sg_hybinc_18_38_250_n_post_b ;
putexcel N4 = char_18_38_300_n_post_b ;
putexcel N5 = char_18_38_300_n_post_se ;
putexcel O4 = sg_char_18_38_300_n_post_b ;
putexcel P4 = hybinc_18_38_300_n_post_b ;
putexcel P5 = hybinc_18_38_300_n_post_se ;
putexcel Q4 = sg_hybinc_18_38_300_n_post_b ;

putexcel B6 = char_18_38_150_dobdd_run_b ;
putexcel B7 = char_18_38_150_dobdd_run_se ;
putexcel C6 = sg_char_18_38_150_dobdd_run_b ;
putexcel D6 = hybinc_18_38_150_dobdd_run_b ;
putexcel D7 = hybinc_18_38_150_dobdd_run_se ;  
putexcel E6 = sg_hybinc_18_38_150_dobdd_run_b ;
putexcel F6 = char_18_38_200_dobdd_run_b ;
putexcel F7 = char_18_38_200_dobdd_run_se ;
putexcel G6 = sg_char_18_38_200_dobdd_run_b ;
putexcel H6 = hybinc_18_38_200_dobdd_run_b ;
putexcel H7 = hybinc_18_38_200_dobdd_run_se ; 
putexcel I6 = sg_hybinc_18_38_200_dobdd_run_b ;  
putexcel J6 = char_18_38_250_dobdd_run_b ;
putexcel J7 = char_18_38_250_dobdd_run_se ;
putexcel K6 = sg_char_18_38_250_dobdd_run_b ;
putexcel L6 = hybinc_18_38_250_dobdd_run_b ;
putexcel L7 = hybinc_18_38_250_dobdd_run_se ;  
putexcel M6 = sg_hybinc_18_38_250_dobdd_run_b ;
putexcel N6 = char_18_38_300_dobdd_run_b ;
putexcel N7 = char_18_38_300_dobdd_run_se ;
putexcel O6 = sg_char_18_38_300_dobdd_run_b ;
putexcel P6 = hybinc_18_38_300_dobdd_run_b ;
putexcel P7 = hybinc_18_38_300_dobdd_run_se ;   
putexcel Q6 = sg_hybinc_18_38_300_dobdd_run_b ;

putexcel B8 = char_18_38_150_postXdobdd_b ;
putexcel B9 = char_18_38_150_postXdobdd_se ;
putexcel C8 = sg_char_18_38_150_postXdobdd_b ;
putexcel D8 = hybinc_18_38_150_postXdobdd_b ;
putexcel D9 = hybinc_18_38_150_postXdobdd_se ;  
putexcel E8 = sg_hybinc_18_38_150_postXdobdd_b ;
putexcel F8 = char_18_38_200_postXdobdd_b ;
putexcel F9 = char_18_38_200_postXdobdd_se ;
putexcel G8 = sg_char_18_38_200_postXdobdd_b ;
putexcel H8 = hybinc_18_38_200_postXdobdd_b ;
putexcel H9 = hybinc_18_38_200_postXdobdd_se ;  
putexcel I8 = sg_hybinc_18_38_200_postXdobdd_b ;
putexcel J8 = char_18_38_250_postXdobdd_b ;
putexcel J9 = char_18_38_250_postXdobdd_se ;
putexcel K8 = sg_char_18_38_250_postXdobdd_b ;
putexcel L8 = hybinc_18_38_250_postXdobdd_b ;
putexcel L9 = hybinc_18_38_250_postXdobdd_se ;  
putexcel M8 = sg_hybinc_18_38_250_postXdobdd_b ;
putexcel N8 = char_18_38_300_postXdobdd_b ;
putexcel N9 = char_18_38_300_postXdobdd_se ;
putexcel O8 = sg_char_18_38_300_postXdobdd_b ;
putexcel P8 = hybinc_18_38_300_postXdobdd_b ;
putexcel P9 = hybinc_18_38_300_postXdobdd_se ;  
putexcel Q8 = sg_hybinc_18_38_300_postXdobdd_b ;

putexcel B10 = char_18_38_150_cons_b ;
putexcel B11 = char_18_38_150_cons_se ;
putexcel C10 = sg_char_18_38_150_cons_b ;
putexcel D10 = hybinc_18_38_150_cons_b ;
putexcel D11 = hybinc_18_38_150_cons_se ; 
putexcel E10 = sg_hybinc_18_38_150_cons_b ;
putexcel F10 = char_18_38_200_cons_b ;
putexcel F11 = char_18_38_200_cons_se ;
putexcel G10 = sg_char_18_38_200_cons_b ;
putexcel H10 = hybinc_18_38_200_cons_b ;
putexcel H11 = hybinc_18_38_200_cons_se ; 
putexcel I10 = sg_hybinc_18_38_200_cons_b ;
putexcel J10 = char_18_38_250_cons_b ;
putexcel J11 = char_18_38_250_cons_se ;
putexcel K10 = sg_char_18_38_250_cons_b ;
putexcel L10 = hybinc_18_38_250_cons_b ;
putexcel L11 = hybinc_18_38_250_cons_se ; 
putexcel M10 = sg_hybinc_18_38_250_cons_b ;
putexcel N10 = char_18_38_300_cons_b ;
putexcel N11 = char_18_38_300_cons_se ;
putexcel O10 = sg_char_18_38_300_cons_b ;
putexcel P10 = hybinc_18_38_300_cons_b ;
putexcel P11 = hybinc_18_38_300_cons_se ;    
putexcel Q10 = sg_hybinc_18_38_300_cons_b ;

/* Figure B5 */

local ysc_charge_18_to_38 	"r(8000 13000)" ;
local ylab_charge_18_to_38 	"8000(1000)13000" ;
local ytitle_charge_18_to_38 "Number of people charged" ;
local title_charge_18_to_38  "Number of people charged" ;

local ysc_hybrid_incar_18_to_38 	"r(1400 2600)" ;
local ylab_hybrid_incar_18_to_38 	"1400(200)2600" ;
local ytitle_hybrid_incar_18_to_38 "Number of people incarcerated" ;
local title_hybrid_incar_18_to_38  "Number of people incarcerated" ;

collapse (sum) `out_list', by(dobwk_run) ;

foreach var in charge_18_to_38 hybrid_incar_18_to_38 { ;
twoway  
	(scatter `var' dobwk_run if abs(dobwk_run) <= 90)
	, 
	title("`title_`var''", size(medsmall)) 
	xtitle("18th birthday relative to August 22, 1996 (weeks)", size(medsmall)) 
	ytitle("`ytitle_`var''", size(medsmall))
	ysc(`ysc_`var'') ylabel(`ylab_`var'')
	legend(off) 
	xscale(r(-90 90)) 
	xlab(-90(30)90)
	ylab(,nogrid)
	graphregion(color(white))
	bgcolor(white)
	xline(0)
	;
	graph export "`output'/figB5_`var'.png", width(1000) height(750) replace ;
} ;

